package com.bw.d1707.springcloud.consumer.controller;

import com.bw.d1707.springcloud.consumer.feign.HelloFeignClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer/")
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HelloFeignClient helloFeignClient;

    @RequestMapping("hello")
    @HystrixCommand(fallbackMethod = "helloFallbackMethod")
    public String hello(@RequestParam("name") String name){
        //return "hello,"+name + ",from consumer";
        /** service-producer服务名称，是application.properties文件定义的spring.application.name的名字 **/
        //return restTemplate.getForObject("http://service-producer/producer/hello?name="+name,String.class);
        return  helloFeignClient.hello(name);
    }

    public String helloFallbackMethod(String name){
        return "接口调用失败:"+name;
    }
}
